package com.yidu.BusinessDealWith.service.impl;

import com.yidu.BusinessDealWith.entity.TaSettlement;
import com.yidu.BusinessDealWith.mapper.TaSettlementMapper;
import com.yidu.BusinessDealWith.service.TaSettlementService;
import com.yidu.TAManagement.entity.TABusiness;
import com.yidu.TAManagement.mapper.TABusinessMapper;
import com.yidu.Util.util.SysUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TaSettlementServiceImpl implements TaSettlementService {

    @Autowired
    private TaSettlementMapper taSettlementMapper;
    @Override
    public int updateTaSettlement(String taSettlement) {
        List<TABusiness> taBusinessList = SysUtil.jsonToArrayList(taSettlement, TABusiness.class);
        String TAId="";
        for (TABusiness t:
             taBusinessList) {
            TAId =t.getTaId();
        }
        return taSettlementMapper.updateTaSettlement(TAId);
    }

    @Override
    public int updateTaSettlementTwo(String taSettlement) {
        List<TABusiness> taBusinessList = SysUtil.jsonToArrayList(taSettlement, TABusiness.class);
        String TAId="";
        for (TABusiness t:
                taBusinessList) {
            TAId =t.getTaId();
        }
        return taSettlementMapper.updateTaSettlementTwo(TAId);
    }

    @Override
    public List<TABusiness> selectTaSettlement(int page, int limit,TaSettlement taSettlement) {
        Map taSettlementMap =new HashMap<>();
        taSettlementMap.put("start",(page-1)*limit);
        taSettlementMap.put("pageRows",limit);
        taSettlementMap.put("strWhere",buildStrWhere(taSettlement));
        List taSettlementList =taSettlementMapper.selectTABusiness(taSettlementMap);
        System.out.println(taSettlementMapper);
        return taSettlementList;
    }

    @Override
    public int total(TaSettlement taSettlement) {
        Map taSettlementMap =new HashMap<>();
        taSettlementMap.put("strWhere",buildStrWhere(taSettlement));
        int total = taSettlementMapper.total(taSettlementMap);
        System.out.println(taSettlementMap.get("strWhere"));
        return total;
    }
    /**
     * 生成SQL条件
     * @param taSettlement 实体类
     * @return  条件的字符串
     */
    public String buildStrWhere(TaSettlement taSettlement){
        StringBuffer strWhere=new StringBuffer("");
        if(taSettlement.getBusinessEndTime()!=null && !taSettlement.getBusinessEndTime().equals(""))
        {
            strWhere.append(" and businessEndTime like '%" + taSettlement.getBusinessEndTime() + "%'");
        }
        if(taSettlement.getBusinessState()!=0 )
        {
            strWhere.append(" and businessState =" + taSettlement.getBusinessState() );
        }
        System.out.println(strWhere);
        System.out.println("1");
        return strWhere.toString();
    }
}
